package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.tanhua.dubbo.mappers.UserMapper;
import com.tanhua.dubbo.server.UserService;
import com.tanhua.model.domain.User;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
public class UserApiImpl implements UserApi {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserService userservice;

    @Override
    public User findByMobile(String mobile) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getMobile, mobile);
        User user = userservice.getOne(queryWrapper);
        return user;
    }

    @Override
    public Long save(User user) {
        userservice.save(user);
        return user.getId();
    }

    @Override
    public void update(User user) {
        userservice.updateById(user);
    }

    @Override
    public User findByUserId(Long userId) {
        return userservice.getById(userId);
    }

    @Override
    public User findByHuanXinId(String huanxinId) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getHxUser, huanxinId);
        User user = userservice.getOne(queryWrapper);
        return user;
    }

    @Override
    public Boolean exist(String phone) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(phone != null, User::getMobile, phone);
        User user = userservice.getOne(queryWrapper);
        return user != null;
    }

    @Override
    public void updatePhone(Long userId, String phone) {
        LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(User::getId, userId);
        updateWrapper.set(User::getMobile, phone);
        userservice.update(updateWrapper);
    }

    @Override
    public Integer cumulativeUsers() {
        return userMapper.cumulativeUsers();
    }

}
